import pygame
import math
import random
# 初始化游戏窗口：
WIDTH = 800
HEIGHT = 600
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Radar Sweep Animation")
clock = pygame.time.Clock()
# 定义颜色：
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# 设置动画的帧率
FPS = 60
# 定义雷达圆圈：
radar_circle_radius = 200
radar_circle_center_x = int(WIDTH / 2)
radar_circle_center_y = int(HEIGHT / 2)
# 定义雷达扫描
radar_sweep_angle = 0
radar_sweep_length = radar_circle_radius + 50
# 定义雷达目标
num_targets = 10
target_radius = 10
targets = []
for i in range(num_targets):
   x = random.randint(radar_circle_center_x - radar_circle_radius,
radar_circle_center_x + radar_circle_radius)
   y = random.randint(radar_circle_center_y - radar_circle_radius,
radar_circle_center_y + radar_circle_radius)
targets.append((x, y))
def update():
   # 在每帧中增加角度
   global radar_sweep_angle
   radar_sweep_angle += 1
# 绘制雷达扫描线
def draw_radar_sweep():
   x = radar_circle_center_x + radar_sweep_length * math.sin(math.radians(radar_sweep_angle))
   y = radar_circle_center_y + radar_sweep_length * math.cos(math.radians(radar_sweep_angle))
   pygame.draw.line(screen, BLACK, (radar_circle_center_x,radar_circle_center_y), (x, y), 3)
# 绘制雷达目标
def draw_radar_targets():
   for target in targets:
      pygame.draw.circle(screen, BLUE, target, target_radius)
      distance_to_target = math.sqrt((target[0] - x) ** 2 + (target[1] - y)** 2)
      if distance_to_target <= target_radius:
         pygame.draw.rect(screen, RED, (target[0], target[1], 60, 20))
         font = pygame.font.SysFont(None, 25)
         text = font.render("DETECTED", True, BLACK)
         screen.blit(text, (target[0], target[1]))
# 主游戏循环
running = True
while running:
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         running = False
   screen.fill(WHITE)
   update()
   draw_radar_sweep()
   draw_radar_targets()
   pygame.display.update()
   clock.tick(FPS)
# 退出游戏
pygame.quit()
